package com.example.tests;

import static com.example.tests.GroupDataGenerator.generateRandomGroups;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;

public class TestBase {
	
	protected static ApplicationManager app;
	

	@BeforeTest 
	public void setUp() throws Exception {
		Properties properties = new Properties();
		properties.load(new FileReader(new File("application.properties")));
		app = new ApplicationManager(properties);
	}

	@AfterTest
	public void tearDown() throws Exception {
		app.stop();

	  }

	@DataProvider
	  public Iterator<Object[]> randomValidGroupGenerator() {
		  List<Object[]> list = wrapGroupsForDataProvider(generateRandomGroups(3));
		  return list.iterator();
	  }
	  


	public static List<Object[]> wrapGroupsForDataProvider(List<GroupData> groups) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups) {
			list.add(new Object[]{group});
		}
		return list;
	}

	public String generateRandomString() {
		  Random rnd = new Random();
		  if (rnd.nextInt(3) == 0) {
		  return "";	 
		  } else {
		   return "test" + rnd.nextInt();
		  }
	  } 
	  
	 /* public String generateRandomMonth() {
		  Random rnd = new Random();
		  String months[] = {"-", "January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
		  int index = rnd.nextInt(months.length);
		  return months[index];
	  }
	  
	  public String generateRandomDay(){
		  Random rnd = new Random();
		  int index = rnd.nextInt(32);
		  if (index == 0) {
			  return "-";
		  } 
		  return ""+index; 
	  } */
		  
	 /* public String generateRandomYear(){
		  Random rnd = new Random();
		  int index = rnd.nextInt(60);
		  if (index == 0) {
			  return "";
		  } 
		  return ""+(index+1950); 
	  } */
	  
	  public String[] generateRandomDate(){
		  int maxDate = 12784; //01.01.2005 in Unix Time divide on 86400 = (1000msec*60sec*60min*24hours)  
		  Random rnd = new Random();
		  int a = rnd.nextInt(maxDate);
		  if (a % 30 == 0) {
			  String randomDate[] = {"-", "-", ""};
			  return randomDate;
		  }
		  long index = (long)a*1000*86400;
		  Calendar mydate = Calendar.getInstance();
		  mydate.setTimeInMillis(index);
		  mydate.getTimeInMillis();
		  String months[] = {"-", "January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
		  String bDay = (""+mydate.get(Calendar.DAY_OF_MONTH));
		  String bMonth = (months[mydate.get(Calendar.MONTH)]);
		  String bYear = (""+mydate.get(Calendar.YEAR));
		  String randomDate[] = {bDay,bMonth, bYear};
		  return randomDate;
		  }
	  
	  @DataProvider
		public Iterator<Object[]> randomValidContactGenerator(){
			List <Object[]> list = new ArrayList<Object[]>();
			Random rnd = new Random(); 
			for (int i= 0; i < 3; i++){
				String[] randomDate = generateRandomDate();
				ContactData contact = new ContactData()
					.withContactname(generateRandomString())
					.withAddress(generateRandomString())
					.withHome(generateRandomString())
					.withMobile(generateRandomString())
					.withWork(generateRandomString())
					.withEmail(generateRandomString())
					.withEmail2(generateRandomString())
					//.withBday("1")
					.withBday(randomDate[0])
					//.withBmonth("April")
					.withBmonth(randomDate[1])
					//.withByear(generateRandomString())
					.withByear(randomDate[2])
					.withGroup("[none]")
					.withAddress2(generateRandomString())
					.withHome2(generateRandomString());
				list.add(new Object[]{contact});
				
			}
			//....
			return list.iterator();
		}
		
	
}
